package com.service.impl;

import com.constant.Constant;
import com.dao.CommentDao;
import com.dao.PraiseDao;
import com.dao.UserDao;
import com.entity.User;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.service.CommentService;
import com.vo.CommentVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * date:2021/4/29
 * description:
 * Author:Stargazer
 */
@Service
public class CommentServiceImpl implements CommentService {
    @Autowired
    private CommentDao commentDao;
    @Autowired
    private UserDao userDao;
    @Autowired
    private PraiseDao praiseDao;

    @Transactional
    @Override
    public int modifyStatusByCommentId(Integer commentId, Integer status) {
        int i = commentDao.updateStatusByCommentId(commentId, status);
        return i;
    }

    @Override
    public PageInfo<CommentVo> findCommentByStatusAndCondition(CommentVo commentVo) {
        Integer pageNo = commentVo.getPageNo();
        Date begin_date = commentVo.getBegin_date();
        Date end_date = commentVo.getEnd_date();
        String context = commentVo.getContext();
        Integer status = commentVo.getStatus();
        PageHelper.startPage(pageNo, Constant.PAGE_SIZE);
        List<CommentVo> commentVos =commentDao.selectCommentByCommentVo(begin_date,end_date,context,status);
        for (CommentVo comment:commentVos) {
            Integer user_id = comment.getUser_id();
            User user = userDao.selectUserById(user_id);
//           遍历集合,将nickName和praiseCount属性查出并赋给commentVos对应的每一个评论对象
            comment.setNickname(user.getNickname());
            comment.setPraise_count(praiseDao.selectPraiseCountByCommentId(comment.getId()));
        }

        return new PageInfo<CommentVo>(commentVos);
    }
}
